/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bungabungaball.player;

import bungabungaball.GameObject;
import bungabungaball.GlobalSettings;
import bungabungaball.PlayerControl;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.MassData;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;

import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;
import org.newdawn.slick.SlickException;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

/**
 * Abstract class for a player.
 *
 * @author Ingo Klose, Stephan Meyer, Sebastian Lehmann
 * @version 1.0
 */
public abstract class Player extends GameObject {

    /**
     * The keys to be pressed to control the player
     */
    protected int[] keyMap = GlobalSettings.keyMapPlayer1;
    /**
     * The players rotation
     */
    public float rotation;
    /**
     * the players speed
     */
    protected float speed;
    /**
     * The players control mode
     */
    public GlobalSettings.ControlMode controlMode = GlobalSettings.ControlMode.Normal;
    /**
     * Images that represent the status of the player:<br/>
     * - is his speed faster/slower<br/>
     * - is his mass higher/lower
     */
    private Image statusImage[];

    /**
     * Constructor
     *
     * @param x
     * @param y
     * @param image
     * @param w
     */
    public Player(int x, int y, Image image, World w) {
        super(x, y, image, w);
        init();
    }

    /**
     * Constructor
     *
     * @param x
     * @param y
     * @param image
     */
    public Player(int x, int y, Image image) {
        super(x, y, image);
        init();
    }

    /**
     * Constructor
     *
     * @param x
     * @param y
     * @param w
     */
    public Player(int x, int y, World w) {
        super(x, y, w);
        init();
    }

    /**
     * Constructor
     *
     * @param x
     * @param y
     */
    public Player(int x, int y) {
        super(x, y);
        init();
    }

    /**
     * Constructor
     *
     * @param image
     * @param w
     */
    public Player(Image image, World w) {
        super(image, w);
        init();
    }

    /**
     * Constructor
     *
     * @param image
     */
    public Player(Image image) {
        super(image);
        init();
    }

    /**
     * Constructor
     *
     * @param w
     */
    public Player(World w) {
        super(w);
        init();
    }

    /**
     * Constructor
     */
    public Player() {
        super();
        init();
    }

    /**
     * Initialize the player
     */
    public void init() {
        speed = GlobalSettings.playerSpeed;
        rotation = GlobalSettings.playerRotation;
        shape = new CircleShape();
        shape.m_radius = 35.f / GlobalSettings.jBox2dFactor;

        fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 1.f;
        fd.friction = 1.f;
        fd.restitution = 1.f;
    }

    /**
     * Get rotation
     *
     * @return The Player rotation
     */
    public double getRichtung() {
        return this.getRotation();
    }

    /**
     * Draw the Player
     *
     * @param g
     */
    @Override
    public void draw(Graphics g) {
    }

    /**
     * Set the Position
     *
     * @param x
     * @param y
     */
    public void setBodyPosition(float x, float y) {
        bodyDef.position.set(x, y);
    }

    /**
     * Set the physicle force
     *
     * @param force
     * @param center
     */
    public void applyForce(Vec2 force, Vec2 center) {
        body.applyForce(force, center);
    }

    /**
     * Move the player
     *
     * @param pc
     * @param delta
     * @param body
     * @param keyMapClientPlayer
     * @throws Exception
     */
    public void move(PlayerControl pc, int delta, Body body, boolean[] keyMapClientPlayer) throws Exception {
        throw new Exception("Not Implemented!");
    }

    /**
     * Set the key map to control the player
     *
     * @param k
     */
    public void setKeyMap(int[] k) {
        keyMap = k;
    }

    /**
     * 
     * Make a friction on the body
     *
     * @param b
     */
    public void makeFriction(Body b) {
        Vec2 newSpeed = b.getLinearVelocity();
        newSpeed.x *= GlobalSettings.reibung;
        newSpeed.y *= GlobalSettings.reibung;

        //b.applyForce(newSpeed, b.getWorldCenter());
        b.setLinearVelocity(newSpeed);


    }

    /**
     * Set a new maximum speed of the player
     *
     * @param s
     */
    public void setSpeed(float s) {
        if (s > GlobalSettings.playerMaxSpeed) {
            speed = GlobalSettings.playerMaxSpeed;
        } else if (s < GlobalSettings.playerMinSpeed) {
            speed = GlobalSettings.playerMinSpeed;
        } else {
            speed = s;
        }
    }

    /**
     * *
     * Get the maximum speed of the player
     *
     * @return maximum speed of the player
     */
    public float getSpeed() {
        return speed;
    }

    /**
     * Set the body of the player
     *
     * @param b
     */
    @Override
    public void setBody(Body b) {
        super.setBody(b);

        MassData m = new MassData();
        m.mass = GlobalSettings.playerMass;
        body.setMassData(m);
    }
}
